<template>
	<view class="default-background">
		<view class="login-page">
			<view class="item-group">
				<view class="login-item" style="border-top-left-radius: 20rpx; border-top-right-radius: 20rpx;border: none;">
					<image src="../../static/logo.png" class="login-logo"></image>
				</view>
				<view class="login-item">
					<view class="item-text">手机号：</view>
					<input class="item-input" v-model="phone" placeholder="请输入手机号"></input>
				</view>
				<view class="login-item">
					<view class="item-text">密码：</view>
					<input class="item-input" v-model="password" placeholder="请输入密码" password="true"></input>
				</view>
				<view class="login-item" style="border-bottom-left-radius: 20rpx; border-bottom-right-radius: 20rpx;">
					<view class="item-text">验证码：</view>
					<input class="item-input" v-model="code" placeholder="请输入验证码"></input>
					<view class="item-code-text" @click="getVerifyCode()" v-if="!codeDisplay">获取验证码</view>
					<image class="item-code-img" @click="getVerifyCode()" :src="codeSrc" v-if="codeDisplay"></image>
				</view>
			</view>
			<view class="btn-group">
				<view class="cancelBtn button" @click="register">注册</view>
				<view class="confirmBtn button" @click="login">登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: "",
				password: "",
				code: "",
				codeSrc: "",
				codeDisplay: false
			}
		},
		onLoad() {
			uni.showToast({
				title: "请先登录",
				icon: "none"
			})
		},
		methods: {
			// 校验手机号和密码格式
			verifyInfo() {
				let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
				if (!reg_tel.test(this.phone)) {
					uni.showToast({
						title: "手机号码格式不正确",
						icon: "none"
					})
					return false;
				}
				if (this.password.length < 6) {
					uni.showToast({
						title: "密码不能小于6位",
						icon: "none"
					})
					return false;
				}
				if (this.password.length > 12) {
					uni.showToast({
						title: "密码不能大于12位",
						icon: "none"
					})
					return false;
				}
				return true;
			},
			
			// 核对是否填写验证码
			verifyCode() {
				if (!this.codeDisplay) {
					uni.showToast({
						title: "请先获取验证码",
						icon: "none"
					});
					return false;
				}
				if (!this.code) {
					uni.showToast({
						title: "请填写验证码",
						icon: "none"
					});
					return false;
				}
				if (this.code.length < 4) {
					uni.showToast({
						title: "验证码不能小于4位",
						icon: "none"
					})
					return false;
				}
				if (this.code.length > 8) {
					uni.showToast({
						title: "验证码不能大于12位",
						icon: "none"
					})
					return false;
				}
				return true;
			},
			
			login() {
				if (!this.verifyInfo()) {
					return;
				}
				
				if (!this.verifyCode()) {
					return;
				}
								
				var that = this;
				this.verifyInfo();
				that.$request.request({
					url: "/user/login",
					method: "POST",
					data: {
						"phone": that.phone,
						"password": that.password,
						"code": that.code,
					}
				}).then(res => {
					console.log(res);
					if (res.code == 200) {
						// 如果返回结果存在 token 则更新本地 token
						if (res.data) {
							uni.setStorageSync("Authorization", res.data.token);
						}
						uni.showToast({
							title: res.message + "，跳转中...",
							icon: "none",
							duration: 2000
						});
						setTimeout(function() {
						  uni.navigateBack()
						}, 1500);
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 2000
						});
					}
				})
			},
			register() {
				if (!this.verifyInfo()) {
					return;
				}
				
				if (!this.verifyCode()) {
					return;
				}
				
				var that = this;
				this.$request.request({
					url: "/user/register",
					method: "POST",
					data: {
						"phone": that.phone,
						"password": that.password,
						"code": that.code
					}
				}).then(res => {
					uni.showToast({
						title: res.message,
						icon: "none"
					})
				})
			},
			
			getVerifyCode() {
				if (!this.verifyInfo()) {
					return;
				}
				var that = this;
				this.$request.request({
					url: "/user/getVerifyCode",
					method: "GET",
					data: {
						"phone": that.phone
					},
					responseType: "arraybuffer",
				}).then(res => {
					that.codeDisplay = true;
					let datas = res;
					that.codeSrc = 'data:image/png;base64,' + btoa(
						new Uint8Array(datas).reduce((datas, byte) => datas + String.fromCharCode(byte), '')
					);
				})
			}
		},
		onBackPress() {
			uni.navigateBack({
				delta: 3
			})
		},
		onBackPress(options) {
			console.log('options')
			console.log(options)
			if (options.from === 'backbutton') {
				return true;
			}
			else {
				return false;
			}
		}
	}
</script>

<style lang="scss" scoped> 
	.login-page {
		
		.login-logo {
			display: block;
			width: 200rpx;
			height: 200rpx;
			background-color: #FFFFFF;
			border-radius: 40rpx;
			margin: 20rpx auto;
			border: 1rpx #F2F1F6 solid;
		}
		
		
		
		.item-group {
			margin: 10rpx 10rpx;
			
			.login-item {
				background-color: #FFFFFF;
				display: flex;
				font-size: 16px;
				line-height: 80rpx;
				// padding: 20rpx 0;
				border-bottom: 1rpx #F2F1F6 solid;
				
				.item-text {
					width: 200rpx;
					text-align: right;
				}
				
				.item-input {
					flex: 1;
					line-height: 80rpx;
					height: 80rpx;
				}
				
				.item-code-text {
						width: 200rpx;
						font-size: 12px;
						color: #4CCFFF;
				}
				
				.item-code-img {
					width: 200rpx;
					height: 80rpx;
				}
			}
		}
	}
</style>
